package com.photographysharing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.photographysharing.annotation.RoleAnnotation;
import com.photographysharing.common.ThreadLocalOperation;
import com.photographysharing.dto.UserDTO;
import com.photographysharing.entity.Users;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.request.AccountRequest;
import com.photographysharing.request.SelectByPaging;
import com.photographysharing.response.DataResponse;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.response.SignResponse;
import com.photographysharing.service.MailService;
import com.photographysharing.service.UsersService;
import com.photographysharing.util.JWTUtil;
import com.photographysharing.util.RedisHelp;
import jakarta.mail.Message;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;

@RequestMapping("/user")
@RestController
public class UserController {

    private UsersService usersService;
    private RedisHelp redisHelp;
    private MailService emaiService;

    public UserController(UsersService usersService, RedisHelp redisHelp, MailService emaiService) {
        this.usersService = usersService;
        this.redisHelp = redisHelp;
        this.emaiService = emaiService;
    }



    @GetMapping("/getUserById")
    public DataResponse<UserDTO> getUserById(String userId){
        Users user = usersService.getById(userId);
        if(user != null){
            UserDTO userDTO = new UserDTO(user);
            DataResponse<UserDTO> res = new DataResponse<>(userDTO);
            return res;
        }
        return  null;
    }

    @GetMapping("/myself")
    public DataResponse<UserDTO> myself(){
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        Users user = usersService.getById(userId);
        if(user != null){
            UserDTO userDTO = new UserDTO(user);
            DataResponse<UserDTO> res = new DataResponse<>(userDTO);
            return res;
        }
        throw new ServiceException(401,"请重新登录!");
    }

    @GetMapping("/selectByPag")
    public DatasResponse<UserDTO> selectByPage(SelectByPaging pag){
        return usersService.selectByPage(pag);
    }

    /**
     * 设置管理员
     * @param id
     * @return
     */
    @RoleAnnotation(type = "superAd")
    @GetMapping("/setAdmin")
    public MessageResponse setAdmin(String id){
        Users user = usersService.getById(id);
        if(user==null){
            return new MessageResponse("Id出错了，你看看怎么个事儿");
        }
        user.setRoleName("admin");
        if(usersService.updateById(user)){
            return new MessageResponse("设置成功!");
        }
        return new MessageResponse("设置失败!",false);
    }

    /**
     * 移除管理员
     * @param id
     * @return
     */
    @RoleAnnotation(type = "superAd")
    @GetMapping("/removeAdmin")
    public MessageResponse removeAdmin(String id){
        Users user = usersService.getById(id);
        if(user==null){
            return new MessageResponse("Id出错了，你看看怎么个事儿");
        }
        user.setRoleName("user");
        if(usersService.updateById(user)){
            return new MessageResponse("移除成功!");
        }
        return new MessageResponse("移除失败!",false);
    }

    @GetMapping("/status")
    public MessageResponse status(String id){
        Users user = usersService.getById(id);
        if(user==null){
            return new MessageResponse("Id出错了，你看看怎么个事儿");
        }
        String msg;
        if(user.getStatus() == 0){
            user.setStatus(1);
            msg = "停用成功!";
        }else{
            msg = "启用成功!";
            user.setStatus(0);

        }
        usersService.updateById(user);
        return new MessageResponse(msg);
    }

    @GetMapping("updateAvatar")
    public MessageResponse updateAvatar(String uuid){
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        Users user = usersService.getById(userId);
        if(user !=null){
            user.setAvatar(uuid);
            if(usersService.updateById(user)){
                return new MessageResponse("修改成功!");
            }
            return  new MessageResponse("修改失败!",false);
        }
        return new MessageResponse(401,"请重新登录!",false);

    }

    @PostMapping("/updateInfo")
    public MessageResponse updateInfo(@RequestBody Users t){
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        Users user = usersService.getById(userId);
        if(user != null){
            user.setName(t.getName());
            user.setUserName(t.getUserName());
            user.setRegion(t.getRegion());
            user.setDescription(t.getDescription());
            if(usersService.updateById(user)){
                return new MessageResponse("修改成功!");
            }
            return new MessageResponse("修改失败!",false);
        }
        return new MessageResponse(401,"请重新登录!",false);
    }



}
